<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="服务名称" prop="serviceName">
        <el-input
          v-model="queryParams.serviceName"
          placeholder="请输入服务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['console:service:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['console:service:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['console:service:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['console:service:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="serviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="服务名称" align="center" prop="serviceName" />
      <el-table-column label="服务图标" align="center" prop="serviceIcon" >
        <template slot-scope="scope">
          <el-image :src="scope.row.serviceIcon" style="width: 50px; height: 50px;"/>
        </template>
      </el-table-column>
      <el-table-column label="服务大类" align="center" prop="serviceClass" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lifeservice_serviceclass" :value="scope.row.serviceClass"/>
        </template>
      </el-table-column>
      <el-table-column label="服务优先级" align="center" prop="priority" />
      <el-table-column label="颜色" align="center" prop="color">
        <template slot-scope="scope">
          <div style="width: 100%;display: flex;justify-content: center;">
            <div style="padding: 2px;border: 1px solid #e6e6e6;">
              <div style="width: 60px;height: 30px;border-radius: 4px;border: 1px solid #999;" :style="{backgroundColor:scope.row.color}"></div>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="当前状态" align="center" prop="isHide" >
        <template slot-scope="scope">
          <span>{{['隐藏','展示'][scope.row.isHide]}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            :icon="scope.row.isHide == 0?'el-icon-reading':'el-icon-notebook-1'"
            @click="isHideHandle(scope.row)"
            v-hasPermi="['console:service:isHide']"
          >{{scope.row.isHide == 0? '展示': '隐藏'}}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['console:service:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['console:service:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改便捷服务信息对话框 -->
    <el-dialog  :close-on-click-modal="false" :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="服务展示优先级" prop="priority">
          <el-input v-model="form.priority" placeholder="请输入服务展示优先级" />
        </el-form-item>
        <el-form-item label="颜色" prop="color">
          <el-color-picker
            v-model="form.color"
            show-alpha>
          </el-color-picker>
        </el-form-item>
        <el-form-item label="服务名称" prop="serviceName">
          <el-input v-model="form.serviceName" placeholder="请输入服务名称" />
        </el-form-item>
        <el-form-item label="服务图标" prop="serviceIcon">
          <el-input v-model="form.serviceIcon" placeholder="请上传服务图标" disabled/>
          <ImageEdit @changeImage="changeImage" :value="form.serviceIcon" :limit="1"></ImageEdit>
        </el-form-item>
        <el-form-item label="服务大类" prop="serviceClass">
          <el-select v-model="form.serviceClass" placeholder="请选择服务大类" clearable>
            <el-option
              v-for="dict in dict.type.lifeservice_serviceclass"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listService, getService, delService, addService, updateService, isHide } from "@/api/console/service";
import ImageEdit from "@/components/ImageEdit/Index";

export default {
  name: "Service",
  dicts: ['lifeservice_serviceclass'],
  components: { ImageEdit },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 便捷服务信息表格数据
      serviceList: [],
      // 服务名称
      names: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serviceName: null,
        serviceIcon: null,
        serviceClass: null,
        priority: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        serviceName: [
          { required: true, message: "服务名称不能为空", trigger: "blur" }
        ],
        priority: [
          { required: true, message: "服务展示优先级不能为空", trigger: "blur" }
        ],
        color: [
          { required: true, message: "颜色不能为空", trigger: "blur" }
        ],
        serviceIcon: [
          { required: true, message: "服务图标不能为空", trigger: "blur" }
        ],
        serviceClass: [
          { required: true, message: "服务大类不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    changeImage($event) {
      this.form.serviceIcon = $event;
    },
    /** 查询便捷服务信息列表 */
    getList() {
      this.loading = true;
      listService(this.queryParams).then(response => {
        this.serviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serviceName: null,
        priority: null,
        color: null,
        serviceIcon: null,
        serviceClass: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.names = selection.map(item => item.serviceName)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加便捷服务信息";
    },
    isHideHandle(row) {
      isHide({id: row.id, isHide: [1, 0][row.isHide]}).then(res=>{
        row.isHide = [1, 0][row.isHide]
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getService(id).then(response => {
        this.form = response.data;
        this.form.serviceClass = String(this.form.serviceClass)
        this.open = true;
        this.title = "修改便捷服务信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateService(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addService(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const names = row.serviceName || this.names;
      this.$modal.confirm('是否确认删除便捷服务信息编号为"' + names + '"的数据项？').then(function() {
        return delService(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('console/service/export', {
        ...this.queryParams
      }, `service_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
